package 匿名内部类实例;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class 按钮的监听器 {
    public static void main(String[] args) {
        //创建窗口
        JFrame window=new JFrame();
        //创建桌布
        JPanel zhuobu=new JPanel();
        window.add(zhuobu);
        //设置按钮
        JButton anniu=new JButton("别点");
        zhuobu.add(anniu);
        //设置窗口大小
        window.setSize(400,300);
        //按钮居中
        window.setLocationRelativeTo(null);
        //展示窗口
        window.setVisible(true);
        //监听器
        //因为ActionListener是接口不能创建对象所以只能用匿名类
        anniu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showConfirmDialog(window,"点你麻痹");
            }
        });
        anniu.addActionListener(e -> JOptionPane.showConfirmDialog(window,"臭傻逼"));//简化
    }
}
